using System;
using System.Text;
using System.IO;
using System.Collections;


namespace CreateScript
{
    class Program
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            try
            {
                ArgumentParser parser = new ArgumentParser(args, false);

                string target = parser.Switch.GetArgumentSwitchValue("t");
                if (string.IsNullOrEmpty(target))
                {
                    WriteUsage();
                    return;
                }

                string directory = parser.Switch.GetArgumentSwitchValue("d");
                if (string.IsNullOrEmpty(directory))
                {
                    directory = Directory.GetCurrentDirectory();
                }

                Directory.SetCurrentDirectory("VBScripts");
                System.Diagnostics.Process.Start("GenerateDBScript.vbs");
                Directory.SetCurrentDirectory("..");

                // Converion folder
                string conversionDirectory = string.Format("{0}\\Conversion\\{1}", directory, target);
                if (!Directory.Exists(conversionDirectory))
                {
                    System.Console.WriteLine(string.Format("Conversion folder {0} could not be found.", conversionDirectory));
                    System.Console.ReadLine();
                    return;
                }


                // First clean the directory where the conversion files will be placed
                string conversionScriptDirectory =
                   string.Format("{0}\\Conversion\\{1}_Scripts", directory, target);
                Directory.CreateDirectory(conversionScriptDirectory);
                CleanDirectory(conversionScriptDirectory);


                int fileOffset = 0;
                WriteConversionFile(conversionDirectory, conversionScriptDirectory, ref fileOffset, true, true);
                WriteConversionFile(directory + "\\Functions", conversionScriptDirectory, ref fileOffset, true, false);
                WriteConversionFile(directory + "\\StoredProcedures", conversionScriptDirectory, ref fileOffset, true, false);
                WriteConversionFile(directory + "\\VBScripts", conversionScriptDirectory, ref fileOffset, true, false);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
                System.Console.WriteLine("\r\n");
                System.Console.ReadLine();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="files"></param>
        /// <param name="filename"></param>
        static void WriteConversionFile(string sourceDirectory,
                                        string destinationDirectory,
                                      ref int fileOffset,
                                      bool newFile,
                                        bool separateSubDirectories)
        {
            string[] conversionFiles = Directory.GetFiles(sourceDirectory, "*.sql");
            if (conversionFiles.Length > 0)
            {
                if (newFile)
                {
                    fileOffset++;
                }

                string filename = string.Format("{0}.sql", fileOffset);

                ArrayList list = new ArrayList(conversionFiles);
                list.Sort();

                string convertFile = string.Format("{0}\\{1}", destinationDirectory, filename);
                foreach (string file in list)
                {
                    string contents = File.ReadAllText(file);
                    File.AppendAllText(convertFile, contents);
                    File.AppendAllText(convertFile, "\r\nGO\r\n\r\n");
                }
            }

            string[] directories = Directory.GetDirectories(sourceDirectory);
            foreach (string subDir in directories)
            {
                bool subNewFile = false;
                if (separateSubDirectories)
                {
                    subNewFile = true;
                }

                WriteConversionFile(subDir, destinationDirectory, ref fileOffset, subNewFile, separateSubDirectories);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="directory"></param>
        static void CleanDirectory(string directory)
        {
            string[] files = Directory.GetFiles(directory, "*.sql");
            foreach (string file in files)
            {
                File.Delete(file);
            }

            string[] directories = Directory.GetDirectories(directory);
            foreach (string subDirectory in directories)
            {
                CleanDirectory(subDirectory);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        static void WriteUsage()
        {
            System.Console.WriteLine("Usage: CreateScript -t target -d directory");
            System.Console.WriteLine(string.Format("target: Sub-directory used to process the conversion; 2.2.0 for example."));
            System.Console.WriteLine(string.Format("directory(optional): Directory used to process the request.  If not provided, the current directory is used."));
            System.Console.WriteLine("Output: Creates a convert.sql file.");
            System.Console.ReadLine();
        }
    }
}
